package com.yys.zproject.controller.sys;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;

import java.io.IOException;

/**
 * @author 14343
 * @version 1.0
 * @date 2025/9/24 21:09
 * @description: TODO
 */
@WebServlet("/sys/captcha")
public class SysCaptchaController extends HttpServlet {
    @Override
    protected void doGet(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) throws IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) throws IOException {

        //1.生成一个验证码的图片流
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(144, 45);
        byte[] imageBytes = captcha.getImageBytes();

        //2.写出去:MIME类型:image/png
        resp.setContentType("image/png");
        resp.getOutputStream().write(imageBytes);
        //3.验证码的文字
        String code = captcha.getCode();
        req.getSession().setAttribute("captcha", code);


    }
}

